package org.martin.snsrs.client.manager;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;

public class Session {
	public final String sessionKey;
	
	public final Integer userID;
	
	public final String username;

	private Session(String sessionKey, Integer userID, String userName, String basedURL){
		this.sessionKey = sessionKey;
		this.userID = userID;
		this.username = userName;
	}
	
	public static void save(Context context, JSONObject session) throws JSONException{
		SharedPreferences.Editor editor = context.getSharedPreferences("snsrs", 0).edit();
		editor.putString("sessionKey", session.getString("sessionKey"));
		editor.putInt("userID", session.getInt("userID"));
		editor.putString("username", session.getString("username"));
		editor.putString("nickname", session.getString("nickname"));
		editor.commit();
	}
	
	public static Session get(Context context){
		SharedPreferences sp = context.getSharedPreferences("snsrs", 0);
		if(sp.getString("sessionKey", "").equals(""))
			return null;
		return new Session(sp.getString("sessionKey", ""),
				sp.getInt("userID", 0),
				sp.getString("username", ""),
				sp.getString("basedURL", ""));
	}
	
	public static void delete(Context context){
		context.getSharedPreferences("snsrs", 0).edit().clear().commit();
	}
}
